//----------------------------------------------------------
//
//   SA:MP Multiplayer Modification For GTA:SA
//   Copyright 2004-2007 SA:MP Team
//
//----------------------------------------------------------
//#include <alloca.h>
//#define _alloca _malloca
#include <malloc.h>
#include "SDK/amx/amx.h"
#include "SDK/plugincommon.h"

#ifndef NULL
#define NULL (0)
#endif

//----------------------------------------------------------

typedef void (*logprintf_t)(const char* format, ...);

static logprintf_t logprintf;
static void **ppPluginData;
extern void *pAMXFunctions;


bool getquery_p(char sIP[],int iPort, int *Qplayers, int *Qmaxplayers);

//----------------------------------------------------------
// The Support() function indicates what possibilities this
// plugin has. The SUPPORTS_VERSION flag is required to check
// for compatibility with the server.

PLUGIN_EXPORT unsigned int PLUGIN_CALL Supports()
{
	return SUPPORTS_VERSION | SUPPORTS_AMX_NATIVES;
}

//----------------------------------------------------------
// The Load() function gets passed on exported functions from
// the SA-MP Server, like the AMX Functions and logprintf().
// Should return true if loading the plugin has succeeded.

PLUGIN_EXPORT bool PLUGIN_CALL Load( void **ppData )
{
	pAMXFunctions = ppData[PLUGIN_DATA_AMX_EXPORTS];
	logprintf = (logprintf_t)ppData[PLUGIN_DATA_LOGPRINTF];

	logprintf( "  Plugin myquery got loaded. Version: " __DATE__ ",  " __TIME__);
	return true;
}

//----------------------------------------------------------
// The Unload() function is called when the server shuts down,
// meaning this plugin gets shut down with it.

PLUGIN_EXPORT void PLUGIN_CALL Unload( )
{
	logprintf( "  Plugin myquery got unloaded." );
}

//----------------------------------------------------------
// This is the sourcecode of the HelloWorld pawn native that we
// will be adding. "amx" is a pointer to the AMX-instance that's
// calling the function (e.g. a filterscript) and "params" is
// an array to the passed parameters. The first entry (params[0])
// is equal to  4 * PassedParameters, e.g. 16 for 4 parameters.

// native HelloWorld();
#define C_LIB //extern "C" __declspec(dllexport)
C_LIB static cell AMX_NATIVE_CALL n_getplayers( AMX* amx, cell* params )
{
	char* ip;
	int port;
	cell *cmaxpl, *cpl;
	amx_StrParam(amx, params[1], ip);
	if(!ip) return false;
	port = (int) params[2];
	amx_GetAddr(amx, params[3], &cmaxpl);
	amx_GetAddr(amx, params[4], &cpl);
	//logprintf("players: %d/%d", pl, maxpl);
	return getquery_p(ip, port, (int *)cpl, (int *)cmaxpl);
}

// And an array containing the native function-names and the functions
// specified with them, e.g. HelloWorld -> n_HelloWorld in this example.
AMX_NATIVE_INFO myfuncsNatives[ ] =
{
	{ "myq_getplayers",			n_getplayers },
	{ 0,					0 }
};

//----------------------------------------------------------
// The AmxLoad() function gets called when a new gamemode or
// filterscript gets loaded with the server. In here we register
// the native functions we like to add to the scripts.

PLUGIN_EXPORT int PLUGIN_CALL AmxLoad( AMX *amx )
{
	return amx_Register( amx, myfuncsNatives, -1 );
}

//----------------------------------------------------------
// When a gamemode is over or a filterscript gets unloaded, this
// function gets called. No special actions needed in here.

PLUGIN_EXPORT int PLUGIN_CALL AmxUnload( AMX *amx )
{
	return AMX_ERR_NONE;
}
